<?php

class Kewenangan_user
{

	private $kd_kewenangan_user;
	private $nip;
	private $kewenangan;
	private $kd_unit;
	private $tgl_mulai;
	private $tgl_selesai;
	private $tgl_buat;
	
	

	function __construct()
	{
	}
	
	function setId($kd_kewenangan_user){
	
		$this->kd_kewenangan_user = $kd_kewenangan_user;
	}
	
	function setNip($nip){
	
		$this->nip = $nip;
	}
	
	function setKewenangan($kewenangan){
	
		$this->kewenangan = $kewenangan;
	}
	
	function setUnit($kd_unit){
	
		$this->kd_unit = $kd_unit;
	}
	
	function setTgl_mulai($tgl_mulai){
	
		$this->tgl_mulai = $tgl_mulai;
	}
	
	function setTgl_selesai($tgl_selesai){
	
		$this->tgl_selesai = $tgl_selesai;
	}
	
	
	function setTgl_buat($tgl_buat){
	
		$this->tgl_buat = $tgl_buat;
	}
	
	
	
	function getNip(){
	
		return $this->nip;
	}
	
	function getId(){
	
		return $this->kd_kewenangan_user;
	}
	
	function getKewenangan(){
	
		return $this->kewenangan;
	}
	
	function getUnit(){
	
		return $this->kd_unit;
	}
	
	function getTgl_mulai(){
	
		return $this->tgl_mulai;
	}
	
	function getTgl_selesai(){
	
		return $this->tgl_selesai;
	}
	
	
	function getTgl_buat(){
	
		return $this->tgl_buat;
	}
}

class Daftar_kewenangan_user{

	function __construct()
	{
	}
	
	function getAll()
	{
		$sql="select * from kewenangan_user order by tgl_buat desc, nip asc ";
		$list_kewenangan_user=array();            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                                			
								$kewenangan_user = new Kewenangan_user();
								$kewenangan_user->setId($row['kd_kewenangan_user']);
                                $kewenangan_user->setNip($row['nip']);
								$kewenangan_user->setKewenangan($row['kewenangan']);
                                $kewenangan_user->setUnit($row['kd_unit']);
                                $kewenangan_user->setTgl_mulai($row['tgl_mulai']);
								$kewenangan_user->setTgl_selesai($row['tgl_selesai']);
								$kewenangan_user->setTgl_buat($row['tgl_buat']);
								
								$list_kewenangan_user[] = $kewenangan_user;
								
                        }
                }       
                return $list_kewenangan_user;

	
	}
	
	function cek_kewenangan_user($nip)
	{
		
		$sql="select count(*) as jml from kewenangan_user where nip='$nip' and tgl_mulai <= CURDATE() and tgl_selesai >= CURDATE()";
        $query=mysql_query($sql);
        $data=mysql_fetch_assoc($query);
        $cek=false;
        if ($data['jml']>0){
          $cek=true;
        }
        return $cek;	
	}
	
	function getById($kd_kewenangan_user)
	{
		$sql="select * from kewenangan_user where kd_kewenangan_user= '$kd_kewenangan_user'";
		$kewenangan_user=false;            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                                $kewenangan_user = new Kewenangan_user();
								$kewenangan_user->setId($row['kd_kewenangan_user']);
                                $kewenangan_user->setNip($row['nip']);
								$kewenangan_user->setKewenangan($row['kewenangan']);
                                $kewenangan_user->setUnit($row['kd_unit']);
                                $kewenangan_user->setTgl_mulai($row['tgl_mulai']);
								$kewenangan_user->setTgl_selesai($row['tgl_selesai']);
								$kewenangan_user->setTgl_buat($row['tgl_buat']);
								
							
								
                        }
                }       
                return $kewenangan_user;

	
	}
	
	function getByNip($nip)
	{
		$sql="select * from kewenangan_user where nip='$nip'";
		$list_kewenangan_user=array();            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                                $kewenangan_user = new Kewenangan_user();
								$kewenangan_user->setId($row['kd_kewenangan_user']);
                                $kewenangan_user->setNip($row['nip']);
								$kewenangan_user->setKewenangan($row['kewenangan']);
                                $kewenangan_user->setUnit($row['kd_unit']);
                                $kewenangan_user->setTgl_mulai($row['tgl_mulai']);
								$kewenangan_user->setTgl_selesai($row['tgl_selesai']);
								$kewenangan_user->setTgl_buat($row['tgl_buat']);
								
								$list_kewenangan_user[] = $kewenangan_user;
								
                        }
                }       
                return $list_kewenangan_user;

	
	}
	
	
	function getKewenanganByNip($nip)
	{
		$sql="select distinct kewenangan from kewenangan_user where nip='$nip' and tgl_mulai <= CURDATE() and tgl_selesai >= CURDATE()";
		$list_kewenangan_user=array();            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                            $list_kewenangan_user[] = $row['kewenangan'];	
                        }
                }       
                return $list_kewenangan_user;
	}
	
	function getUnitKewenangan($nip,$kewenangan)
	{
		$sql="select distinct kd_unit from kewenangan_user where nip='$nip' 
		and kewenangan='$kewenangan' and tgl_mulai <= CURDATE() and tgl_selesai >= CURDATE()";
		$list_kewenangan_user=array();            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                                			
								$list_kewenangan_user[] = $row['kd_unit'];
								
                        }
                }       
                return $list_kewenangan_user;

	
	}
	
	function getNipByUnitKewenangan($unit,$kewenangan)
	{
		$sql="select * from kewenangan_user where kd_unit='$unit' 
		and kewenangan='$kewenangan'";
		$list_kewenangan_user=array();            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                                			
								$kewenangan_user = new Kewenangan_user();
								$kewenangan_user->setId($row['kd_kewenangan_user']);
                                $kewenangan_user->setNip($row['nip']);
								$kewenangan_user->setKewenangan($row['kewenangan']);
                                $kewenangan_user->setUnit($row['kd_unit']);
                                $kewenangan_user->setTgl_mulai($row['tgl_mulai']);
								$kewenangan_user->setTgl_selesai($row['tgl_selesai']);
								$kewenangan_user->setTgl_buat($row['tgl_buat']);
								
								$list_kewenangan_user[] = $kewenangan_user;
								
                        }
                }       
                return $list_kewenangan_user;

	
	}
	
	function getNipByKewenangan($kewenangan)
	{
		$sql="select * from kewenangan_user where kewenangan='$kewenangan'";
		$list_kewenangan_user=array();            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                                			
								$kewenangan_user = new Kewenangan_user();
								$kewenangan_user->setId($row['kd_kewenangan_user']);
                                $kewenangan_user->setNip($row['nip']);
								$kewenangan_user->setKewenangan($row['kewenangan']);
                                $kewenangan_user->setUnit($row['kd_unit']);
                                $kewenangan_user->setTgl_mulai($row['tgl_mulai']);
								$kewenangan_user->setTgl_selesai($row['tgl_selesai']);
								$kewenangan_user->setTgl_buat($row['tgl_buat']);
								
								$list_kewenangan_user[] = $kewenangan_user;
								
                        }
                }       
                return $list_kewenangan_user;

	
	}
	
	function getNipByUnitKewenanganAtasan($unit,$kewenangan)
	{
		$sql="select a.* 
		from kewenangan_user a, unit b
		where 
		a.kd_unit=b.kd_unit AND 
		b.kd_parent='$unit' AND
		a.kewenangan='$kewenangan' 
		";
		$list_kewenangan_user=array();            
                $data = mysql_query($sql);
                if($data){
                        while($row = mysql_fetch_assoc($data)){
                                			
								$kewenangan_user = new Kewenangan_user();
								$kewenangan_user->setId($row['kd_kewenangan_user']);
                                $kewenangan_user->setNip($row['nip']);
								$kewenangan_user->setKewenangan($row['kewenangan']);
                                $kewenangan_user->setUnit($row['kd_unit']);
                                $kewenangan_user->setTgl_mulai($row['tgl_mulai']);
								$kewenangan_user->setTgl_selesai($row['tgl_selesai']);
								$kewenangan_user->setTgl_buat($row['tgl_buat']);
								
								$list_kewenangan_user[] = $kewenangan_user;
								
                        }
                }       
                return $list_kewenangan_user;

	
	}
	
	function add(Kewenangan_user $kewenangan_user){
	
		$sql="
		insert into kewenangan_user(kd_kewenangan_user, nip, kewenangan, kd_unit, tgl_mulai, tgl_selesai, tgl_buat)
		values(
		'',
		'".$kewenangan_user->getNip()."',
		'".$kewenangan_user->getKewenangan()."',
		'".$kewenangan_user->getUnit()."',
		'".$kewenangan_user->getTgl_mulai()."',
		'".$kewenangan_user->getTgl_selesai()."',
		'".$kewenangan_user->getTgl_buat()."'
		)
		";
		$query=mysql_query($sql);
		return $query;
	}
	
	function edit(Kewenangan_user $kewenangan_user){
		$sql="
			UPDATE kewenangan_user
			SET
			nip='".$kewenangan_user->getNip()."',
			kewenangan='".$kewenangan_user->getKewenangan()."',
			kd_unit='".$kewenangan_user->getUnit()."',
			tgl_mulai='".$kewenangan_user->getTgl_mulai()."',
			tgl_selesai='".$kewenangan_user->getTgl_selesai()."'
			WHERE
			kd_kewenangan_user='".$kewenangan_user->getId()."'
		";
		$query=mysql_query($sql);
		return $query;
	
	
	}
	
	function delete(Kewenangan_user $kewenangan_user){
		$sql="
			delete from kewenangan_user
			WHERE
			kd_kewenangan_user='".$kewenangan_user->getId()."'
		";
		$query=mysql_query($sql);
		return $query;
	
	
	}
	
	
	
	

}